// pages/books/books.js
const db = wx.cloud.database()
const substance = db.collection('substance')
const user = db.collection('user')
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hig: 80,
    columnsGood: ['电子产品', '体育用品', '生活用品', '其他'],
    goodList: null
  },
  handleCardEntry(e) {
    wx.navigateTo({
      url: '../show/detailed/detailed?_id=' + e.currentTarget.dataset._id + '&_openid=' + e.currentTarget.dataset._openid
    }) // 带参跳转
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(123)
    let self = this;
    substance.where({
      tip: 'good'
    }).get({
      success: res => {
        let goodList = res.data
        let handleTime = (date) => {
          let nowTime = (new Date()).valueOf()
          if (parseInt((nowTime - date) / 2592000000) >= 1) {
            return parseInt((nowTime - date) / 2592000000) + '月前'
          } else if (parseInt((nowTime - date) / 86400000) >= 1) {
            return parseInt((nowTime - date) / 86400000) + '天前'
          } else if (parseInt((nowTime - date) / 3600000) >= 1) {
            return parseInt((nowTime - date) / 3600000) + '小时前'
          } else if (parseInt((nowTime - date) / 60000) >= 1) {
            return parseInt((nowTime - date) / 60000) + '分钟前'
          } else {
            return '刚刚'
          }
        }
        for (let i = 0; i < goodList.length; i++) {
         
          goodList[i].type = self.data.columnsGood[goodList[i].type];
          goodList[i].date = handleTime(goodList[i].date)
        }
        // res.data 是包含以上定义的两条记录的数组
        self.setData({
          goodList: goodList.reverse()
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  gettiplist(event) {
    let self = this;
    let index = event.currentTarget.dataset.idx + ""
    substance.where({
      tip: 'good'
    }).where({
      type: index
    }).get({
      success: res => {
        let goodList = res.data
        let handleTime = (date) => {
          let nowTime = (new Date()).valueOf()
          if (parseInt((nowTime - date) / 2592000000) >= 1) {
            return parseInt((nowTime - date) / 2592000000) + '月前'
          } else if (parseInt((nowTime - date) / 86400000) >= 1) {
            return parseInt((nowTime - date) / 86400000) + '天前'
          } else if (parseInt((nowTime - date) / 3600000) >= 1) {
            return parseInt((nowTime - date) / 3600000) + '小时前'
          } else if (parseInt((nowTime - date) / 60000) >= 1) {
            return parseInt((nowTime - date) / 60000) + '分钟前'
          } else {
            return '刚刚'
          }
        }
        for (let i = 0; i < goodList.length; i++) {
          
          goodList[i].type = self.data.columnsGood[goodList[i].type];
          goodList[i].date = handleTime(goodList[i].date)
        }
        // res.data 是包含以上定义的两条记录的数组
        self.setData({
          goodList: goodList.reverse()
        })
      }
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})